package com.lv.service.impl;

import com.lv.mapper.TbGoodsDescMapper;
import com.lv.mapper.TbGoodsMapper;
import com.lv.mapper.TbItemMapper;
import com.lv.model.TbGoods;
import com.lv.model.TbGoodsDesc;
import com.lv.model.TbItem;
import com.lv.model.TbItemExample;
import com.lv.service.GoodsService;
import com.lv.vo.DescVo;
import com.lv.vo.GoVo;
import com.lv.vo.GoodsVo;
import com.lv.vo.TemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private TbGoodsMapper gm;
    @Resource
    private TbGoodsDescMapper dm;
    @Resource
    private TbItemMapper im;


    @Transactional
    @Override
    public void add(GoVo vo, List<TbItem> title, List<TbItem> l) {


        System.out.println(l.get(0));
        //取出GOODS
        TbGoods goods = new TbGoods();
        goods.setSellerId("lv");
        BeanUtils.copyProperties(vo.getGoods(), goods);
        goods.setSellerId(vo.getSellName());
        //增加goods表  返回主键ID
        gm.insertSelective(goods);

        //取出DESC
        TbGoodsDesc desc = new TbGoodsDesc();
        BeanUtils.copyProperties(vo.getDesc(), goods);
        //把Id放ru
        desc.setGoodsId(goods.getId());
        //增加dexc表
        dm.insertSelective(desc);
        String goodsId = String.valueOf(goods.getId());

        for (int i = 0; i < l.size(); i++) {
            l.get(i).setSellerId(goodsId);
            l.get(i).setCreateTime(new Date());
            l.get(i).setUpdateTime(new Date());
            l.get(i).setCategoryid(goods.getBrandId());
            l.get(i).setStatus("1");
            l.get(i).setTitle(title.get(i).getTitle());
            im.insertSelective(l.get(i));
        }

    }

    //商品详情查询

    @Override
    public GoodsVo getGoodsDetail(Long goodsId) {
        //根据ID查询商品Goods表数据
        TbGoods goods = gm.selectByPrimaryKey(goodsId);
        GoodsVo vo = new GoodsVo();
        BeanUtils.copyProperties(goods,vo);

        //根据spucID查询商品详情数据
        TbGoodsDesc desc = dm.selectByPrimaryKey(goodsId);
        DescVo descVo = new DescVo();
        BeanUtils.copyProperties(desc,descVo);
        vo.setDescVo(descVo);

        //根据goodsId查询Item表数据
        TbItemExample example = new TbItemExample();
        TbItemExample.Criteria criteria = example.createCriteria();
        criteria.andGoodsIdEqualTo(goodsId);
        List<TbItem> tbItems = im.selectByExample(example);
        List<TemVo> itemVoList = tbItems.stream().map(item -> {
            TemVo itemVo = new TemVo();
            BeanUtils.copyProperties(item, itemVo);
            return itemVo;
        }).collect(Collectors.toList());
        //将sku集合存放到GoodsVo中
        vo.setItemVoList(itemVoList);
        return vo;
    }

    //增加购物车 根据商品Id去查询商品的数据

    @Override
    public TemVo getItem(Long itemId) {
        TbItem item = im.selectByPrimaryKey(itemId);
        TemVo vo =  new TemVo();
        BeanUtils.copyProperties(item, vo);
        return vo;
    }
}